package com.example.mysaat.service_menu_manual_test.AllFunctions;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.example.mysaat.R;
import com.example.mysaat.handle_utils.HandlerFile;
import com.example.mysaat.service_menu_manual_test.allActivities.AutoPressActivity;
//这个类的目的是得到battery id,TA,Battery

public class Battery {
    private String batteryType = null;
    private Activity activity = null;
    private static final String TAG = "Battery";
    private static final int CHECK_CABLE_CONNECTION = 0;

    private String batteryStatus = null;
    private String batteryCapacity = null;
    private String batteryTemperature = null;
    private String batteryVoltage = null;
    private String BatteryCurrent = null;
    private String batteryID = null;
    private Boolean batteryChange = false;
    public Boolean isRegister = false;

    private static final String STATUS_FILE = "/sys/class/power_supply/battery/status";
    private static final String TEMPERATURE_FILE = "/sys/class/power_supply/battery/temp";
    private static final String BATTERY_LEVEL = "/sys/class/power_supply/battery/capacity";
    private static final String BATTERY_ID_FILE = "/sys/class/power_supply/battery/present";
    private static final String BATTERY_VOL_N = "/sys/class/power_supply/battery/voltage_now";
    private static final String BATTERY_TYPE_FILE = "/sys/class/power_supply/bms/battery_type";
    private static final String BATTERY_CURRENT_NOW = "/sys/class/power_supply/battery/current_now";

    public Battery (Activity activity){
        this.activity = activity;
        Log.d(TAG, "Battery: "+batteryType);
    }

    public String getBatteryStatus() {
        return batteryStatus;
    }

    public void setBatteryStatus(String batteryStatus) {
        this.batteryStatus = batteryStatus;
    }

    public String getBatteryCapacity() {
        return batteryCapacity;
    }

    public void setBatteryCapacity(String batteryCapacity) {
        this.batteryCapacity = batteryCapacity;
    }

    public void setBatteryTemperature(String batteryTemperature) {
        this.batteryTemperature = batteryTemperature;
    }

    public void setBatteryVoltage(String batteryVoltage) {
        this.batteryVoltage = batteryVoltage;
    }

    public String getBatteryCurrent() {
        return BatteryCurrent;
    }

    public void setBatteryCurrent(String batteryCurrent) {
        BatteryCurrent = batteryCurrent;
    }

    public String getBatteryID() {
        return batteryID;
    }

    public void setBatteryID(String batteryID) {
        this.batteryID = batteryID;
    }

    public Boolean getBatteryChange() {
        IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        this.activity.registerReceiver(broadcastReceiver,filter);
        this.isRegister = true;
        return batteryChange;
    }

    public void setBatteryChange(Boolean batteryChange) {
        this.batteryChange = batteryChange;
    }

    public void getBatteryType(){
        batteryType = HandlerFile.getFileInfo(BATTERY_TYPE_FILE);
        if(batteryType==null||"Unknown Battery".equals(batteryType)){
            Log.d(TAG, "batteryType:"+batteryType==null?"file not find":batteryType);
        }else{
            Log.d(TAG, "batteryType:"+batteryType);
        }
    }


    public void  checkPowerInfo(){
        StringBuffer powerInfo = new StringBuffer();
        setBatteryStatus(HandlerFile.getFileInfo(STATUS_FILE));
        setBatteryCapacity(HandlerFile.getFileInfo(BATTERY_LEVEL)+"%");
        setBatteryTemperature(getBatteryTemperature()+"C");
        setBatteryVoltage(getBatteryVoltage()+"mV");
        setBatteryCurrent(getBatteryCurrentQcom()+"mA");
        setBatteryID(HandlerFile.getFileInfo(BATTERY_ID_FILE));
    }

    //get temperature of battery
    private String getBatteryTemperature(){
        try {
        int temp =Integer.valueOf(HandlerFile.getFileInfo(TEMPERATURE_FILE)).intValue()/10;
        return temp+"";
        } catch (NumberFormatException e) {
            return 0 + "";
        }
    }
    //get voltage of battery
    private String getBatteryVoltage(){
        int vol = 0;
        try {
            vol = Integer.valueOf(HandlerFile.getFileInfo(BATTERY_VOL_N)) / 1000;
        }catch (Exception e){
            e.printStackTrace();
        }
        return vol+"";
    }

    private int getBatteryCurrentQcom() {
        String cur_str = HandlerFile.getFileInfo(BATTERY_CURRENT_NOW);
        try {
            return (-Integer.valueOf(cur_str).intValue()) / 1000;
        } catch (NumberFormatException e) {
            return 0;
        }
    }

    private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
           String action = intent.getAction();
           if(Intent.ACTION_BATTERY_CHANGED.equals(action)){
               // 0 means we are discharging, anything else means charging
               boolean isCharging = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1) != 0;
               setBatteryChange(isCharging);
               Log.d(TAG, "检测充电线: 广播开动了");
               ((AutoPressActivity)activity).updateCableConnectStatus();
           }
        }
    };

    public void mUnregisterBatteryChangeBroadcast(){
        this.activity.unregisterReceiver(broadcastReceiver);
    }

    /*public static boolean isCharging(Context context) {
        Intent batteryBroadcast = context.registerReceiver(null,
                new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
        // 0 means we are discharging, anything else means charging
        boolean isCharging = batteryBroadcast.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1) != 0;
        Toast.makeText(context,""+isCharging,Toast.LENGTH_LONG).show();
        return isCharging;
    }*/

}
